document.addEventListener('DOMContentLoaded', () => {
    const addressInput = document.getElementById('addressInput');
    const fillBtn = document.getElementById('fillBtn');
    const clearBtn = document.getElementById('clearBtn');
    const statusMessage = document.getElementById('statusMessage');

    // 清空按钮功能
    clearBtn.addEventListener('click', () => {
        addressInput.value = '';
        showMessage('输入已清空', 'success');
    });

    // 填充按钮功能
    fillBtn.addEventListener('click', () => {
        const address = addressInput.value.trim();

        if (!address) {
            showMessage('请输入地址信息', 'error');
            return;
        }

        showMessage('正在处理地址信息...', 'loading');

        // 与后台脚本通信
        chrome.runtime.sendMessage(
            { action: "fillAddress", address: address },
            (response) => {
                if (response.success) {
                    showMessage(`地址已成功填充到京东订单页面！<br>街道信息请核对[${response.selectAdd}]`, 'success');
                   
                } else {
                    showMessage(`填充失败：${response.error || '未知错误'}`, 'error');
                }
            }
        );
    });


    

    // 消息显示函数
    function showMessage(text, type) {
        statusMessage.innerHTML = text;
        statusMessage.className = 'status-message ' + type;
        statusMessage.style.display = 'block';

        // 3秒后自动隐藏
        setTimeout(() => {
            statusMessage.style.display = 'none';
        }, 3000);
    }
});